package com.cxf.ws.clien;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Component
public class PublishEndpoint implements ApplicationRunner{

	@Autowired
	private WebApplicationContext webApplicationConnect;
	
	@Autowired()
	@Qualifier(Bus.DEFAULT_BUS_ID)
	private SpringBus bus;
	
	@SuppressWarnings("resource")
	@Override
	public void run(ApplicationArguments applicationArguments) throws Exception {
		//当把应用打成一个jar包运行的时候，后面跟着的命令行参数可以通过ApplicationArguments拿到
		System.out.println("开始进行自动发布web service接口");
		String[] beanNames = webApplicationConnect.getBeanNamesForAnnotation(AutoPublish.class);
		for(String beanName : beanNames) {
			String publishAddr = webApplicationConnect.getType(beanName).getAnnotation(AutoPublish.class).publishAddr();
			EndpointImpl endpointImpl = new EndpointImpl(bus, webApplicationConnect.getBean(beanName));
			endpointImpl.publish(publishAddr);
			System.out.println(String.format("发布的接口地址：[%s]", publishAddr));
		}
		System.out.println("完成web service接口自动发布");
	}

}
